Nodejs removing event listeners [migrated]

Posted by JeffH on Programmers See other posts from Programmers or by JeffH
Published on 2012-04-15T18:59:09Z Indexed on 2012/04/15 23:44 UTC
Read the original article Hit count: 182

Filed under:

Looking to get some help. I'm new to Nodejs and wondering if it is possible, to remove this custom event emitter. Most of this code comes from the Hand on nodejs by Pedro Teixeira. My function at the bottom is attempting to remove the custom event emitter you setup in the book.

var util = require('util');
var EventEmitter = require('events').EventEmitter;

// Pseudo-class named ticker that will self emit every 1 second.
var Ticker = function()
{
    var self = this;
    setInterval(function()
    {
        self.emit('tick');
    }, 1000);   
};

// Bind the new EventEmitter to the sudo class.
util.inherits(Ticker, EventEmitter);

// call and instance of the ticker class to get the first
// event started. Then let the event emitter run the infinante loop.
var ticker = new Ticker();
ticker.on('tick', function()
{
    console.log('Tick');
});

(function tock()
{
    setInterval(function()
    {
        console.log('Tock');
        EventEmitter.removeListener('Ticker',function()
            {
                console.log("Clocks Dead!");
            });
    }, 5000);
})();

© Programmers or respective owner

Related posts about nodejs